package org.asiainfo.ability.manager.service;

import org.asiainfo.ability.gateway.dao.ServiceDao;
import org.asiainfo.ability.gateway.model.MicroService;
import org.asiainfo.ability.gateway.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: visen
 * @Date: 2018/3/21
 * @Description:
 */
@Service
public class MsService {

    @Autowired
    private ServiceDao serviceDao;


    @Transactional(propagation = Propagation.REQUIRED, readOnly = true,
            isolation = Isolation.DEFAULT, timeout = 16000, rollbackFor = Exception.class)
    public Map listServicesViewData(Page page) {
        List<MicroService> microServices = serviceDao.listMicroServices(page);
        for (MicroService microService : microServices) {
            int count = serviceDao.getInsCount(microService.getServiceName());
            if (count > 0) {
                microService.setInsSummary(serviceDao.getActiveInsCount(microService.getServiceName()) + " / " + count);
            }
        }

        Map ret = new HashMap<>();
        ret.put("data", microServices);
        ret.put("code", 0);
        ret.put("msg", "");
        ret.put("count", serviceDao.getServiceCount());
        return ret;
    }
}
